package community.hospital.service.domain.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import community.hospital.entity.Menu;
import community.hospital.entity.RoleMenuRelation;
import community.hospital.entity.request.ListMenuRequest;
import community.hospital.service.domain.MenuService;
import community.hospital.mapper.MenuMapper;
import community.hospital.service.domain.RoleMenuRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author ydj
* @description 针对表【menu】的数据库操作Service实现
* @createDate 2023-01-16 16:35:35
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{

    @Resource
    private RoleMenuRelationService roleMenuRelationService;
    @Resource
    private MenuMapper menuMapper;

    @Override
    public List<Menu> listMenuByRoleId(Long roleId) {
        List<Long> menuIds = roleMenuRelationService
                .listByRoleId(roleId)
                .stream()
                .map(RoleMenuRelation::getMenuId)
                .collect(Collectors.toList());
        return CollUtil.isEmpty(menuIds)?null:listByIds(menuIds);
    }

    @Override
    public List<Menu> listMenuByRoleIds(List<Long> roleIds) {
        Set<Long> menuIds = roleMenuRelationService.lambdaQuery()
                .in(roleIds != null, RoleMenuRelation::getRoleId, roleIds)
                .list()
                .stream()
                .map(RoleMenuRelation::getMenuId)
                .collect(Collectors.toSet());
        return CollUtil.isEmpty(menuIds) ? new ArrayList<>() : listByIds(menuIds);
    }

    @Override
    public List<Map<Long, Menu>> mapMenuByRoleIds(List<Long> roleIds) {
        return menuMapper.mapMenuByRoleIds(roleIds);
    }

    @Override
    public Page<Menu> listMenu(ListMenuRequest request) {
        Page<Menu> menuPage = new Page<>(request.getPageNumber(), request.getPageSize());
        return lambdaQuery()
                .eq(Menu::getMenuKey, request.getMenuKey())
                .eq(Menu::getPid, request.getPid())
                .page(menuPage);
    }
}




